package com.hhkj.study.admin.user.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.hhkj.study.admin.user.entity.User;
import com.hhkj.study.admin.user.excel.UserExcelRes;
import com.hhkj.study.admin.user.model.req.UserCloseReq;
import com.hhkj.study.admin.user.model.req.UserReq;
import com.hhkj.study.admin.user.model.res.UserRes;
import com.hhkj.study.admin.user.service.biz.UserBizService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.base.biz.controller.v3.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.ExcelImportBaseTemplate;
import com.ruoyi.common.valid.VAdd;
import com.ruoyi.common.valid.VDisable;
import com.ruoyi.common.valid.VEdit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* UserController
*
* @author SERVER
* @since 2024-09-06
*/
@Slf4j
@RestController
@PermissionPrefix("user:user")
@RequestMapping("/admin/customer/user")
public class UserController extends BaseController<User, UserReq, UserRes, UserBizService> {

    @PreAuthorize("@ss.hasPermi(this, ':export')")
    @PostMapping("/export")
    public void export(@RequestBody UserReq req, HttpServletResponse response) {
        try {
            getBizService().export(req, response);
        } catch (GlobalException e) {
            throw new GlobalException(e.getMessage());
        } catch (Exception e) {
            log.error("导出文件失败", e);
            throw new GlobalException("系统异常，文件导出失败，请稍后重试");
        }
    }

    /**
     * 导入
     * @return
     */
    @PreAuthorize("@ss.hasPermi(this, ':import')")
    @PostMapping("/import")
    public AjaxResult importCustomerUser(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        return AjaxResult.success(getBizService().importCustomerUser(file, request));
    }

    /** 禁用/启用 */
    @PreAuthorize("@ss.hasPermi(this, 'edit')")
    @PutMapping("/disable")
    public AjaxResult updateDisable(@Validated(VDisable.class) @RequestBody UserCloseReq req) {
        return toAjax(this.getBizService().updateDisable(req));
    }

    /** 删除 */
    @PreAuthorize("@ss.hasPermi(this, 'remove')")
    @DeleteMapping("/remove")
    public AjaxResult removeByIdList(@Validated(VAdd.class) @RequestBody UserCloseReq req)
    {
        return toAjax(this.getBizService().remove(req));
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi(this, 'edit')")
    @PostMapping("/rePassword/{id}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult rePassword(@PathVariable Long id) {
        return toAjax(this.getBizService().rePassword(id));
    }

    @Override
    public ExcelImportBaseTemplate getExcelImportBaseTemplate() {
        return new UserExcelRes();
    }
}
